#! /usr/bin/python2.7

import sys
import os
import subprocess
import signal
import shlex
import getpass

try:
    input = raw_input
except NameError:
    pass


os.environ['DLC'] = r'/apps/oe117/dlc'
os.environ['TERM'] = 'xterm'
#p = subprocess.Popen(["stty", "intr", "^c"])
#p.wait()
p = subprocess.Popen(". %s; echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH; echo SHLIB_PATH=$SHLIB_PATH; echo LIBPATH=$LIBPATH; echo PROLIBPATH=$PROLIBPATH" % os.path.join(r'/apps/oe117/dlc', 'bin', 'slib_env'), stdout=subprocess.PIPE, shell=True, universal_newlines=True)
output = p.communicate()[0]
environ = dict(line.split("=", 1) for line in output.splitlines())
os.environ.update(environ)

os.environ['PATH'] = os.path.join(r'/apps/oe117/dlc', 'bin') + ':' + os.environ['PATH']

os.environ[r'AVMMEMORY2'] = r'1'


if 'PROMSGS' not in os.environ:
    os.environ['PROMSGS'] = os.path.join(r"/apps/oe117/dlc", "promsgs")

if 'PROTERMCAP' not in os.environ:
    os.environ['PROTERMCAP'] = os.path.join(r"/apps/oe117/dlc", "protermcap")
os.environ['PS1'] = str(os.getpid())
os.environ['PROPATH'] = r'/apps/ee2022/crp/config,/apps/ee2022/crp/build/catalog/packages/qracore/3/23/2/0/qad.qra.core/bin/qracore.pl,/apps/ee2022/crp/dist/mfg-customizations,/apps/ee2022/crp/dist/fin/proxypatch,/apps/ee2022/crp/dist/pro/com/mfgpro,/apps/oe117/dlc/tty/netlib/OpenEdge.Net.pl,/apps/ee2022/crp/build/catalog/packages/fin-bin64-proxy/2022/1/6/1/proxy.pl,/apps/ee2022/crp/dist/pro,.,/apps/ee2022/crp/build/catalog/packages/service-interface-foundation/1/1/220/0/lib/qra.pl,/apps/ee2022/crp/dist/mfg,/apps/ee2022/crp/dist/mfg/us,/apps/ee2022/crp/dist/mfg/us/bbi'
os.environ['APPDIR'] = r'/apps/ee2022/crp'

#if len(sys.argv) > 1:
#    codepage = sys.argv[1]
#else:
codepage = 'cp936'
startup_params = ''


if codepage == 'utf-8':
    startup_params = '-ucc -utf8intatchar ' + startup_params


#if 'TERM' not in os.environ:
#    os.environ['TERM'] = input("Please enter your terminal type: ")


#if 'USER' not in os.environ:
#    os.environ['USER'] = getpass.getuser()

program = os.path.expandvars(r'com/qad/qra/core/ClientBootstrap.p -param startup=/home/mfg/scripts/xxautosend_po_expire_price.p,logfile=/apps/ee2022/crp/build/logs/client/session-chui-${USER}.log')

params = [os.path.join(r"/apps/oe117/dlc", "bin", "_progres"), "-pf", r'/apps/ee2022/crp/config/bootstrap.pf'] + startup_params.split() + ["-cpinternal", codepage, "-cpstream", codepage, "-p"] + program.split()  + ["-b"]
pcwd = os.path.abspath(os.path.expandvars(os.path.expanduser(r'/apps/ee2022/crp/build/work/client-chui')))
p = subprocess.Popen(params, cwd=pcwd)

#signal.signal(signal.SIGINT, signal.SIG_IGN)
#p.wait()
